package com.chatcompetition.controller;


import com.chatcompetition.constant.JwtClaimsConstant;
import com.chatcompetition.domain.entity.Employee;
import com.chatcompetition.domain.vo.EmployeeLoginVO;
import com.chatcompetition.properties.JwtProperties;
import com.chatcompetition.service.EmployeeService;
import com.chatcompetition.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;
    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody Employee employee) {
        Employee emp = employeeService.login(employee.getEmployeeNumber(), employee.getEmployeePassword(), employee.getEmployeeDepartment());


        Integer employeeStatus = employeeService.getEmployeeStatusByEmployeeNumber(employee.getEmployeeNumber());
        if(employeeStatus == null || employeeStatus!= 1)
        {
            return ResponseEntity.status(401).body("Invalid credentials");
        }
        EmployeeLoginVO loginVO = new EmployeeLoginVO();

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getEmployeeNumber());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        loginVO.setToken(token);
        loginVO.setName(emp.getName());
        loginVO.setEmployeeDepartment(emp.getEmployeeDepartment());
        loginVO.setEmployeeNumber(emp.getEmployeeNumber());


        System.out.println(token);
        if (loginVO != null) {
            return ResponseEntity.ok(loginVO);
        } else {
            return ResponseEntity.status(401).body("Invalid credentials");
        }
    }


    @GetMapping("/list")
    public ResponseEntity<List<Employee>> getAllEmployees() {
        List<Employee> employees = employeeService.getAllEmployees();
        return ResponseEntity.ok(employees);
    }

    @PostMapping("/add")
    public ResponseEntity<Employee> addEmployee(@RequestBody Employee employee) {
        employee.setEmployeeStatus(1);
        Employee newEmployee = employeeService.addEmployee(employee);
        return ResponseEntity.ok(newEmployee);
    }

    // 根据工号更新员工信息
    @PutMapping("/update")
    public boolean updateEmployeeByEmployeeNumber(@RequestBody Employee employee) {
        return employeeService.updateEmployeeByEmployeeNumber(employee);
    }

    // 根据工号删除员工
    @DeleteMapping("/delete/{employeeNumber}")
    public boolean deleteByEmployeeNumber(@PathVariable String employeeNumber) {
        return employeeService.deleteByEmployeeNumber(employeeNumber);


    }

    // 根据工号更新员工状态
    @PutMapping("/updateStatus/{employeeNumber}/{employeeStatus}")
    public boolean updateEmployeeStatusByEmployeeNumber(
            @PathVariable String employeeNumber,
            @PathVariable int employeeStatus
    ) {
        // 直接从路径参数中获取状态值
        return employeeService.updateEmployeeStatusByEmployeeNumber(employeeNumber, employeeStatus);
    }



    // 根据工号查询员工的 employee_status
    @GetMapping("/status/{employeeNumber}")
    public ResponseEntity<Integer> getEmployeeStatusByEmployeeNumber(@PathVariable String employeeNumber) {
        Integer status = employeeService.getEmployeeStatusByEmployeeNumber(employeeNumber);
        if (status != null) {
            return ResponseEntity.ok(status);
        } else {
            return ResponseEntity.status(404).body(null);
        }
    }
}